In [1]:
import pandas as pd
import numpy as np
import folium
from folium import plugins
In [ ]:
df = pd.read_csv("../Data/Scratch/Accidents/2018_accidents_gu_bcn.csv")
df = df.append(pd.read_csv("../Data/Scratch/Accidents/2017_accidents_gu_bcn.csv"))
df = df.append(pd.read_csv("../Data/Scratch/Accidents/2016_accidents_gu_bcn.csv"))
df = df.append(pd.read_csv("../Data/Scratch/Accidents/2019_accidents_gu_bcn.csv"))

df.Any.loc[df.Any.isna() == True] = 2019

df['weight'] = df['Numero_morts'] * 1+df['Numero_lesionats_greus'] * 0.5+df['Numero_lesionats_lleus'] * 0.2
normalized_column =(df.weight-df.weight.min())/(df.weight.max()-df.weight.min())
df['weight'] = normalized_column

#Processing the Points
features = []
for index, row in df.iterrows():
    if row.Numero_morts > 0:
        append_Feature_Point(features, row, "red")
    else:
            if row.Numero_lesionats_greus > 1:
                append_Feature_Point(features, row, "orange")
            else:
                    if row.Numero_lesionats_lleus > 0:
                        append_Feature_Point(features, row, "yellow")
                    else:
                        append_Feature_Point(features, row, "white")
In [86]:
def create_Marker_Table(row):
    table = ""
    table = table + "<table style='width:100%'>" + "<tr><th>Numero Expedient</th><td>"+row.Numero_expedient+"</td>"+"<tr><th>Causa Accident</th><td>"+row.Descripcio_causa_vianant+"</td>"+"<tr><th>Data de l'Accident</th><td>"+str(row.Hora_dia)+"h "+str(row.Dia_mes)+"/"+str(row.Mes_any)+"/"+str(row.Any)+"</td>"+"<tr><th>Dia de la Setmana</th><td>"+row.Descripcio_dia_setmana+"</td>"+"<tr><th>Numero de Morts</th><td>"+str(row.Numero_morts)+"</td>"+"<tr><th>Numero de Lesionats Greus</th><td>"+str(row.Numero_lesionats_greus)+"</td>"+"<tr><th>Numero de Lesionats Lleus</th><td>"+str(row.Numero_lesionats_lleus)+"</td>"+"<tr><th>Numero de Victimes</th><td>"+str(row.Numero_victimes)+"</td>"+"<tr><th>Numero de Vehicles Implicats</th><td>"+str(row.Numero_vehicles_implicats)+"</td>"+"</tr>"
    return table

def append_Feature_Point(featurejson,row,color):
    featurejson.append(
        {
            'type': 'Feature',
            'geometry': {
                'type': 'Point',
                'coordinates': [row.Longitud,row.Latitud],
            },
            'properties': {
                'time': str(int(row.Any))+"-"+str(row.Mes_any)+"-"+str(row.Dia_mes),
                'popup': create_Marker_Table(row),
            'icon': 'circle',
                'iconstyle': {
                    'color': color,
                    'fillColor': 'grey',
                    'fillOpacity': 0.8,
                    'radius': 2
                },
                'weight': row.weight
            }
        }
    )
In [85]:
#Creating the Map                       
barcelona_map = folium.Map([41.3947,2.1557], zoom_start=12.4, tiles='cartodbpositron')


plugins.TimestampedGeoJson(
    {
        'type': 'FeatureCollection',
        'features': features
    },
    period='P1D',
    add_last_point=True,
    auto_play=False,
    loop=False,
    max_speed=1,
    loop_button=True,
    date_options='YYYY/MM/DD',
    time_slider_drag_update=True,
    duration='P1D'
).add_to(barcelona_map)

folium.TileLayer('openstreetmap').add_to(barcelona_map)
folium.TileLayer('Stamen Terrain').add_to(barcelona_map)

folium.LayerControl().add_to(barcelona_map)  

#Plotting
barcelona_map
Out[85]:
Make this Notebook Trusted to load map: File -> Trust Notebook